/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package planocontas.bd;

import conexao.ConexaoSingleton;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import planocontas.classes.PlanoContas;

/**
 *
 * @author Gabi
 */
public class PlanoContasDAO {

     private EntityManager manager;
     private Query query;

     public PlanoContasDAO() throws Exception{
         manager = ConexaoSingleton.getConexao();
     }

     public PlanoContas getPlanoContas(Integer codigo){
     PlanoContas planoContas = manager.find(PlanoContas.class, codigo);
     return planoContas;

     }
     public List<PlanoContas> getPlanoContas(){
     query = manager.createQuery("select pc from conta pc order by idConta");
     return query.getResultList();
     }

     public void salvar(PlanoContas planoContas){
         manager.getTransaction().begin();
         manager.persist(planoContas);
         manager.getTransaction().commit();
     }
     public void excluir(PlanoContas planoContas){
         manager.getTransaction().begin();
         manager.remove(planoContas);
         manager.getTransaction().commit();
     }

     public List<PlanoContas> pesquisaPlanoContas(String descricao){
         query = manager.createNamedQuery("planoPorDescricao");
         query.setParameter("descricaoPlanoContas", "%" + descricao + "%");
         return query.getResultList();
    }
}
